home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Sample.bin
/
FileChooserDemoFrame.java
< prev
next >
Wrap
Text File
|
1998-11-01
|
12KB
|
374 lines
/*
A GUI version of the FileChooser sample from SUN.
This frame sets the options for the JFileChooser
*/
import com.sun.java.swing.*;
import com.sun.java.swing.preview.*;
import com.sun.java.swing.preview.filechooser.*;
import java.awt.*;
import java.beans.*;
import java.io.*;
import com.symantec.itools.swing.JLookAndFeelComboBox;
public class FileChooserDemoFrame extends com.sun.java.swing.JFrame
{
ButtonGroup group1 = new ButtonGroup();
ButtonGroup group2 = new ButtonGroup();
ButtonGroup group3 = new ButtonGroup();
public FileChooserDemoFrame()
{
// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
setTitle("File Chooser Demo");
getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
getContentPane().setFont(new Font("Dialog", Font.PLAIN, 0));
setSize(525,305);
setVisible(false);
JPanel1.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
getContentPane().add(JPanel1);
JPanel1.setBounds(0,0,525,61);
openOption.setText("Open");
openOption.setActionCommand("Open");
openOption.setSelected(true);
JPanel1.add(openOption);
openOption.setBounds(5,5,57,25);
saveOption.setText("Save");
saveOption.setActionCommand("Save");
JPanel1.add(saveOption);
saveOption.setBounds(67,5,55,25);
customOption.setText("Custom");
customOption.setActionCommand("Custom");
JPanel1.add(customOption);
customOption.setBounds(127,5,71,25);
customMessage.setText("DoIt");
customMessage.setColumns(20);
customMessage.setEnabled(false);
JPanel1.add(customMessage);
customMessage.setBounds(203,8,220,19);
customMessage.setSelectionEnd(4);
customMessage.setSelectionStart(4);
customMessage.setCaretPosition(4);
JPanel2.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
getContentPane().add(JPanel2);
JPanel2.setBounds(0,61,525,61);
noFilterOption.setText("No Filtering");
noFilterOption.setActionCommand("No Filtering");
noFilterOption.setSelected(true);
JPanel2.add(noFilterOption);
noFilterOption.setBounds(5,5,90,25);
addFilterOption.setText("Add JPG and GIF Filters");
addFilterOption.setActionCommand("Add JPG and GIF Filters");
JPanel2.add(addFilterOption);
addFilterOption.setBounds(100,5,157,25);
previewOption.setText("Show Preview");
previewOption.setActionCommand("Show Preview");
previewOption.setSelected(true);
JPanel2.add(previewOption);
previewOption.setBounds(262,5,108,25);
JPanel3.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
getContentPane().add(JPanel3);
JPanel3.setBounds(0,122,525,61);
hiddenFileOption.setText("Show Hidden Files");
hiddenFileOption.setActionCommand("Show Hidden Files");
JPanel3.add(hiddenFileOption);
hiddenFileOption.setBounds(5,5,130,25);
extensionsOption.setText("Show Extensions");
extensionsOption.setActionCommand("Show Extensions");
extensionsOption.setSelected(true);
JPanel3.add(extensionsOption);
extensionsOption.setBounds(140,5,125,25);
fileViewOption.setText("Use FileView");
fileViewOption.setActionCommand("Use FileView");
fileViewOption.setSelected(true);
JPanel3.add(fileViewOption);
fileViewOption.setBounds(270,5,99,25);
JPanel4.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
getContentPane().add(JPanel4);
JPanel4.setBounds(0,183,525,61);
filesOption.setText("Just Select Files");
filesOption.setActionCommand("Just Select Files");
filesOption.setSelected(true);
JPanel4.add(filesOption);
filesOption.setBounds(5,5,120,25);
directoriesOption.setText("Just Select Directories");
directoriesOption.setActionCommand("Just Select Directories");
JPanel4.add(directoriesOption);
directoriesOption.setBounds(130,5,157,25);
filesAndDirectoriesOption.setText("Select files or Directories");
filesAndDirectoriesOption.setActionCommand("Select files or Directories");
JPanel4.add(filesAndDirectoriesOption);
filesAndDirectoriesOption.setBounds(292,5,171,25);
JPanel5.setLayout(new FlowLayout(FlowLayout.CENTER,20,5));
getContentPane().add(JPanel5);
JPanel5.setBounds(0,244,525,61);
showFileChooser.setText("Show FileChooser");
showFileChooser.setActionCommand("Show FileChooser");
showFileChooser.setMnemonic((int)'S');
JPanel5.add(showFileChooser);
showFileChooser.setBounds(141,5,137,25);
JPanel5.add(setLookAndFeelComboBox);
setLookAndFeelComboBox.setBounds(298,5,86,24);
//}}
group1.add(openOption);
group1.add(saveOption);
group1.add(customOption);
group2.add(noFilterOption);
group2.add(addFilterOption);
group3.add(filesOption);
group3.add(directoriesOption);
group3.add(filesAndDirectoriesOption);
//{{INIT_MENUS
//}}
//{{REGISTER_LISTENERS
SymWindow aSymWindow = new SymWindow();
this.addWindowListener(aSymWindow);
SymItem lSymItem = new SymItem();
customOption.addItemListener(lSymItem);
SymAction lSymAction = new SymAction();
showFileChooser.addActionListener(lSymAction);
//}}
}
static public void main(String args[])
{
JFrame jf=new FileChooserDemoFrame();
jf.setLocation(50,50);
jf.setVisible(true);
}
//{{DECLARE_CONTROLS
com.sun.java.swing.JPanel JPanel1 = new com.sun.java.swing.JPanel();
com.sun.java.swing.JRadioButton openOption = new com.sun.java.swing.JRadioButton();
com.sun.java.swing.JRadioButton saveOption = new com.sun.java.swing.JRadioButton();
com.sun.java.swing.JRadioButton customOption = new com.sun.java.swing.JRadioButton();
com.sun.java.swing.JTextField customMessage = new com.sun.java.swing.JTextField();
com.sun.java.swing.JPanel JPanel2 = new com.sun.java.swing.JPanel();
com.sun.java.swing.JRadioButton noFilterOption = new com.sun.java.swing.JRadioButton();
com.sun.java.swing.JRadioButton addFilterOption = new com.sun.java.swing.JRadioButton();
com.sun.java.swing.JCheckBox previewOption = new com.sun.java.swing.JCheckBox();
com.sun.java.swing.JPanel JPanel3 = new com.sun.java.swing.JPanel();
com.sun.java.swing.JCheckBox hiddenFileOption = new com.sun.java.swing.JCheckBox();
com.sun.java.swing.JCheckBox extensionsOption = new com.sun.java.swing.JCheckBox();
com.sun.java.swing.JCheckBox fileViewOption = new com.sun.java.swing.JCheckBox();
com.sun.java.swing.JPanel JPanel4 = new com.sun.java.swing.JPanel();
com.sun.java.swing.JRadioButton filesOption = new com.sun.java.swing.JRadioButton();
com.sun.java.swing.JRadioButton directoriesOption = new com.sun.java.swing.JRadioButton();
com.sun.java.swing.JRadioButton filesAndDirectoriesOption = new com.sun.java.swing.JRadioButton();
com.sun.java.swing.JPanel JPanel5 = new com.sun.java.swing.JPanel();
com.sun.java.swing.JButton showFileChooser = new com.sun.java.swing.JButton();
com.symantec.itools.swing.JLookAndFeelComboBox setLookAndFeelComboBox = new com.symantec.itools.swing.JLookAndFeelComboBox();
//}}
//{{DECLARE_MENUS
//}}
class SymWindow extends java.awt.event.WindowAdapter
{
public void windowClosing(java.awt.event.WindowEvent event)
{
Object object = event.getSource();
if (object == FileChooserDemoFrame.this)
FileChooserDemoFrame_windowClosing(event);
}
}
void FileChooserDemoFrame_windowClosing(java.awt.event.WindowEvent event)
{
System.exit(0);
}
class SymItem implements java.awt.event.ItemListener
{
public void itemStateChanged(java.awt.event.ItemEvent event)
{
Object object = event.getSource();
if (object == customOption)
customOption_itemStateChanged(event);
}
}
void customOption_itemStateChanged(java.awt.event.ItemEvent event)
{
if(customOption.isSelected())
{
customMessage.setEnabled(true);
}
else
{
customMessage.setEnabled(false);
}
}
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == showFileChooser)
showFileChooser_actionPerformed(event);
}
}
void showFileChooser_actionPerformed(java.awt.event.ActionEvent event)
{
JFileChooser fc = new JFileChooser();
//Select Dialog type
if(openOption.isSelected())
{
fc.setDialogType(JFileChooser.OPEN_DIALOG);
}
else if (saveOption.isSelected())
{
fc.setDialogType(JFileChooser.SAVE_DIALOG);
}
else if (customOption.isSelected())
{
fc.setDialogType(JFileChooser.CUSTOM_DIALOG);
fc.setApproveButtonText(customMessage.getText());
}
//setup for filters
if(addFilterOption.isSelected())
{
ExampleFileFilter jpgFilter=new ExampleFileFilter("jpg", "JPEG Compressed Image Files"),
gifFilter=new ExampleFileFilter("gif", "GIF Image Files"),
bothFilter=new ExampleFileFilter(new String[] {"jpg", "gif"}, "JPEG and GIF Image Files");
//setup for extensions
jpgFilter.setExtensionListInDescription(extensionsOption.isSelected());
gifFilter.setExtensionListInDescription(extensionsOption.isSelected());
bothFilter.setExtensionListInDescription(extensionsOption.isSelected());
//add filters
fc.resetChoosableFileFilters();
fc.addChoosableFileFilter(jpgFilter);
fc.addChoosableFileFilter(gifFilter);
fc.addChoosableFileFilter(bothFilter);
}
//setup for previewing
if(previewOption.isSelected())
{
fc.setAccessory(new FilePreviewer(fc));
}
//setup for hidden files
fc.setFileHidingEnabled(!hiddenFileOption.isSelected());
//setup for file view
if(fileViewOption.isSelected())
{
ExampleFileView fileView = new ExampleFileView();
fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));
fc.setFileView(fileView);
}
//decide on acceptable selection types
if(filesOption.isSelected())
{
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
}
else if(directoriesOption.isSelected())
{
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
else if(filesAndDirectoriesOption.isSelected())
{
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
//Display the dialog, and tell the user which file he picked.
if (fc.showDialog(this, null ) == JFileChooser.APPROVE_OPTION)
{
File theFile = fc.getSelectedFile();
if(theFile!=null)
{
if(theFile.isDirectory())
{
JOptionPane.showMessageDialog(this, "You chose this directory: " + theFile.getAbsolutePath() );
}
else
{
JOptionPane.showMessageDialog(this, "You chose this file: " + theFile.getAbsolutePath());
}
}
}
}
class FilePreviewer extends JComponent implements PropertyChangeListener {
ImageIcon thumbnail = null;
File f = null;
public FilePreviewer(JFileChooser fc)
{
setPreferredSize(new Dimension(100, 50));
fc.addPropertyChangeListener(this);
}
public void loadImage()
{
if(f != null)
{
ImageIcon tmpIcon = new ImageIcon(f.getPath());
if(tmpIcon.getIconWidth() > 90)
{
thumbnail = new ImageIcon(
tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
}
else
{
thumbnail = tmpIcon;
}
}
}
public void propertyChange(PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
{
f = (File) e.getNewValue();
if(isShowing())
{
loadImage();
repaint();
}
}
}
public void paint(Graphics g) {
if(thumbnail == null)
{
loadImage();
}
if(thumbnail != null)
{
int x = getWidth()/2 - thumbnail.getIconWidth()/2;
int y = getHeight()/2 - thumbnail.getIconHeight()/2;
if(y < 0)
{
y = 0;
}
if(x < 5)
{
x = 5;
}
thumbnail.paintIcon(this, g, x, y);
}
}
}
}